<template>
  <div class="cs-page-container">

    <div class="cs-filter-container">
      <el-input
        v-model.trim="keyword"
        clearable
        placeholder="关键词"
        style="width:100px"
      />
      <el-button
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="handleAdd"
      >添加
      </el-button>
    </div>
    <div class="cs-filter-result">
      <el-table
        v-loading="listLoading"
        :data="list"
        :height="'calc(100vh - 300px)'"
        border
        fit
        size="small"
        :row-key="row => row.id"
        highlight-current-row
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
      >
        <el-table-column
          align="center"
          label="关键字"
        >

          <template slot-scope="{ row }">
            {{ row.text }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          fixed="right"
          width="160"
        >
          <template v-if="!row.read" slot-scope="{row}">
            <el-button
              type="primary"
              size="mini"
              @click="handleDel(row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>

  import { xhrIllegalKeyword, xhrIllegalKeywordDel, xhrIllegalKeywordAdd } from '@api/anchor'
  import AnchorMixin from '../../mixin'

  export default {
    name: 'RoleList',
    filters: {},
    mixins: [AnchorMixin],
    data() {
      return {
        list: [],
        total: 0,
        keyword: null
      }
    },

    created() {
      this.doSearch()
    },
    methods: {
      doSearch() {
        xhrIllegalKeyword().then(resp => {
          this.list = resp.data
        })
      },
      handleAdd() {
        if (!this.keyword) {
          this.$message.warning('请填写添加的关键字,')
          return
        }
        xhrIllegalKeywordAdd({ keyword: this.keyword }).then(resp => {
          this.doSearch()
          this.$message.success('添加成功')
        })
      },

      handleDel(row) {
        xhrIllegalKeywordDel(row.id).then(resp => {
          this.doSearch()
        })
      }
    }
  }
</script>
